package com.novellotus.iov.carvice.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Rich on 2018/1/9.
 */

public class TimeUtils {


    /**
     * 将时间戳转换为年月日时分秒格式
     */
    public static String stampTotime(String stamp) {
        if (stamp != null && !stamp.equals("") && !stamp.equals("null")) {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = Long.valueOf(stamp);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            return res;
        } else {
            return " - -";
        }

    }

    /**
     * 时间戳转年月日
     *
     * @return
     */
    public static String StampToDate(String stamp) {
        if (stamp != null && !stamp.equals("") && !stamp.equals("null")) {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long lt = Long.valueOf(stamp);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            return res;
        } else {
            return "";
        }
    }

    /**
     * 年月日转时间戳
     *
     * @return
     */
    public static long dateToStamps(String s) {
        long timeStamp = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = sdf.parse(s);
            timeStamp = d.getTime();
            //   timeStamp = String.valueOf(l);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }
}
